package com.zjp.birthdaymanager;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.zjp.birthdaymanager.fragment.BirthdayFragment;
import com.zjp.birthdaymanager.fragment.BlessingFragment;
import com.zjp.birthdaymanager.fragment.MyCenterFragment;
import com.zjp.birthdaymanager.viewpager.MyPagerAdapter;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends FragmentActivity implements ViewPager.OnPageChangeListener {
    private ViewPager mViewPager;
    private List<Fragment> mFragments;
    private MyPagerAdapter mPagerAdapter;//曾江平更改
    private RadioGroup radioGroup;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        mViewPager = (ViewPager) findViewById(R.id.viewpager);
        radioGroup = (RadioGroup) findViewById(R.id.rg_bottom);
        RadioButton rb = (RadioButton) radioGroup.getChildAt(0);
        rb.performClick();//模拟点击事件
        initdata();
    }

    private void initdata() {
        mFragments = new ArrayList<Fragment>();
        BirthdayFragment birthdayFragment = new BirthdayFragment();
        BlessingFragment blessingFragment = new BlessingFragment();
        MyCenterFragment myCenterFragment = new MyCenterFragment();

        mFragments.add(birthdayFragment);
        mFragments.add(blessingFragment);
        mFragments.add(myCenterFragment);

        mPagerAdapter = new MyPagerAdapter(getSupportFragmentManager(),mFragments);
        mViewPager.setAdapter(mPagerAdapter);

        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                int position = 0;
                switch (checkedId) {
                    case R.id.rb_birth:
                        position = 0;
                        break;
                    case R.id.rb_bless:
                        position = 1;
                        break;

                    case R.id.rb_center:
                        position = 2;
                        break;

                }
                mViewPager.setCurrentItem(position);
            }
        });
        mViewPager.addOnPageChangeListener(this);
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        // 更改radioButton的选中项
        RadioButton radioButton = (RadioButton) radioGroup.getChildAt(position);
        radioButton.setChecked(true);
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
}
